package xuan.gui;

import java.util.LinkedList;
import javax.swing.JTree;
import org.w3c.dom.Node;
import org.roblybarger.*;

import xuan.lib.Path;

/**
 * @author xuan
 */
public class TreeUtils {
	/**
	 * expand all internal tree
	 * @param tree
	 */
	public static void expandAll(JTree tree){
		for (int i = 0; i < tree.getRowCount(); i++) {
	         tree.expandRow(i);
		}
	}
	
	/**
	 * expand internal tree to a specific node
	 * @param tree the internal tree
	 * @param treeNode the node to expand
	 */
	public static void expandOneNode(JTree tree, XMLTreeNode treeNode) {
		Node node = treeNode.getElement();
		Path path = new Path(node);
		LinkedList<Integer> pathList = path.getComponents();
		int row = 0;
		
		for(int i=1;i<pathList.size();i++){
			row += pathList.get(i) + 1;
			tree.expandRow(row);
		}
	}

	
	/**
	 * expand tree to 2 specific nodes
	 * @param tree the internal tree
	 * @param inserted the inserted node to expand
	 * @param deleted the deleted node to expand
	 */
	public static void expandTwoNodes(JTree tree, XMLTreeNode inserted, XMLTreeNode deleted) {
		Node node1 = inserted.getElement();
		Node node2 = deleted.getElement();
		
		Path path1 = new Path(node1);
		Path path2 = new Path(node2);
		
		
		//expand the most at left node before
		if(path1.isRightOf(path2)){
			Path path3 = path1;
			path1 = path2;
			path2 = path3;
		}
		
		LinkedList<Integer> pathList = path2.getComponents();
		int row = 0;		
		for(int i=1;i<pathList.size();i++){
			row += pathList.get(i) + 1;
			tree.expandRow(row);
		}		
		
		pathList = path1.getComponents();
		row = 0;		
		for(int i=1;i<pathList.size();i++){
			row += pathList.get(i) + 1;
			tree.expandRow(row);
		}
		
	}
}
